/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package testAgent.behaviours.equation;

import jade.core.behaviours.DataStore;
import jade.core.behaviours.OneShotBehaviour;
import jade.core.behaviours.SequentialBehaviour;
import testAgent.behaviours.WaitForSomeoneHaveSolvedBehaviours;
import testAgent.behaviours.equation.partThree.CreateMultiplicativeEquationBehaviour;
import testAgent.behaviours.equation.partThree.CreateSummativeEquationBehaviour;
import testAgent.behaviours.responder.EquationSolverResponderBehaviours;
import udes.ds.agent.MultiplicativeEquation;

/**
 * @author Cody Stoutenburg
 * 
 */
public class SolveMultiplicativeEquationBehaviour extends SequentialBehaviour {

	private static final long serialVersionUID = -573935812375645522L;

	private static final String FIRST_EQUATION = "equation1";
	private static final String SECOND_EQUATION = "equation2";

	private static final String FIRST_DERIVATED_EQUATION = "equation1Derivated";
	private static final String SECOND_DERIVATED_EQUATION = "equation2Derivated";

	private static final String FPRIME_G = "equation f' g";
	private static final String GPRIME_F = "equation g' f";

	private static final String FINISH_EQUATION = "result";

	private final String resultKey;
	private final DataStore childDatastore;

	public SolveMultiplicativeEquationBehaviour(
			MultiplicativeEquation equation, String resultKey) {
		super();
		this.resultKey = resultKey;

		childDatastore = getDataStore();
		childDatastore.put(FIRST_EQUATION, equation.getFirst());
		childDatastore.put(SECOND_EQUATION, equation.getSecond());

		// behaviours that request result
		this.addSubBehaviour(new WaitForSomeoneHaveSolvedBehaviours(
				childDatastore, equation.getFirst(), FIRST_DERIVATED_EQUATION));
		// behaviours that request result of second
		this.addSubBehaviour(new WaitForSomeoneHaveSolvedBehaviours(
				childDatastore, equation.getSecond(), SECOND_DERIVATED_EQUATION));

		this.addSubBehaviour(new CreateMultiplicativeEquationBehaviour(
				childDatastore, FIRST_DERIVATED_EQUATION, SECOND_EQUATION,
				FPRIME_G));
		this.addSubBehaviour(new CreateMultiplicativeEquationBehaviour(
				childDatastore, FIRST_EQUATION, SECOND_DERIVATED_EQUATION,
				GPRIME_F));

		this.addSubBehaviour(new CreateSummativeEquationBehaviour(
				childDatastore, FPRIME_G, GPRIME_F, FINISH_EQUATION));

		this.addSubBehaviour(new solveBehaviours());
	}

	private void finishSolveEquation() {
		getDataStore().put(resultKey, childDatastore.get(FINISH_EQUATION));
		getDataStore().put(EquationSolverResponderBehaviours.IS_CALCUL_FINISH,
				0);
	}

	private class solveBehaviours extends OneShotBehaviour {

		private static final long serialVersionUID = -2770911657283715286L;

		@Override
		public void action() {
			finishSolveEquation();
		}
	}

}
